package com.ruoyi.sbxm.xmjh.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbxm.xmjh.domain.XmjhFj;
import com.ruoyi.sbxm.xmjh.domain.Xmjh;
import com.ruoyi.sbxm.xmjh.service.IXmjhService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目计划Controller
 *
 * @author huacai
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/sbxm/xmjh")
public class XmjhController extends BaseController {
    @Resource
    private IXmjhService xmjhService;

    /**
     * 查询项目计划列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Xmjh xmjh) {
        startPage();
        List<Xmjh> list = xmjhService.selectXmjhList(xmjh);
        return getDataTable(list);
    }

    /**
     * 导出项目计划列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:export')")
    @Log(title = "项目计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Xmjh xmjh) {
        List<Xmjh> list = xmjhService.selectXmjhList(xmjh);
        ExcelUtil<Xmjh> util = new ExcelUtil<Xmjh>(Xmjh.class);
        util.exportExcel(response, list, "项目计划数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Xmjh> util = new ExcelUtil<Xmjh>(Xmjh.class);
        util.importTemplateExcel(response, "项目计划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "项目计划", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Xmjh> util = new ExcelUtil<Xmjh>(Xmjh.class);
        InputStream inputStream = file.getInputStream();
        List<Xmjh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = xmjhService.batchInsertXmjh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取项目计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:query')")
    @GetMapping(value = "/{xmjhId}")
    public AjaxResult getInfo(@PathVariable("xmjhId") String xmjhId) {
        return success(xmjhService.selectXmjhByXmjhId(xmjhId));
    }

    /**
     * 新增项目计划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:add')")
    @Log(title = "项目计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Xmjh xmjh) {
        xmjh.setUserId(Math.toIntExact(getUserId())); //用户ID
        xmjh.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        xmjh.setCreateBy(getUsername()); //创建人
        xmjh.setIdWhere("存在");
        return toAjax(xmjhService.insertXmjh(xmjh));
    }

    /**
     * 修改项目计划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:edit')")
    @Log(title = "项目计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Xmjh xmjh) {
        return toAjax(xmjhService.updateXmjh(xmjh));
    }

    /**
     * 删除项目计划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:xmjh:remove')")
    @Log(title = "项目计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{xmjhIds}")
    public AjaxResult remove(@PathVariable String[] xmjhIds) {
        return toAjax(xmjhService.deleteXmjhByXmjhIds(xmjhIds));
    }

    /**
     * 获得项目计划主键ID (UUID)
     */
    @GetMapping("/getXmJhId")
    public AjaxResult getXmJhId() {
        String getXmJhId = IdUtils.fastSimpleUUID();
        return success(getXmJhId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertXmjhFjByXmjhId(@RequestBody XmjhFj xmjhFj) {
        return toAjax(xmjhService.insertXmjhFjByXmjhId(xmjhFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectXmjhFjList(String sId) {
        XmjhFj xmjhFj = new XmjhFj();
        xmjhFj.setXmjhId(sId);
        return AjaxResult.success(xmjhService.selectXmjhFjList(xmjhFj));
    }

    /**
     * 删除项目计划附件
     */
    @Log(title = "项目计划附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteXmjhFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(xmjhService.deleteXmjhFjByFjIds(fjIds));
    }

}
